package com.junzhiit.nengji.cms.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.bo.ShopTechnicianBO;
import com.junzhiit.nengji.cms.service.ShopTechnicianService;
import com.junzhiit.nengji.cms.web.exception.ShopTechnicianNotFoundException;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.util.aliyun.AliyunOSSClientUtil;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.ShopTechnicianVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.dao.ShopTechnicianDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.ShopTechnician;

@Service
public class ShopTechnicianServiceImpl extends BaseServiceImpl implements ShopTechnicianService {
	
	@Autowired
	private ShopTechnicianDAO shopTechnicianDAO;
	
	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;
	
	@Value("${upload.mall.shop.technician}")
	private String shopTechnicianPath;
	
	@Value("${upload.temp}")
	private String uploadPath;
	
	//阿里云API的bucket名称
	@Value("${oss.backet.name}")
	private String backetName;

	@Override
	public ResultVO getList(String sessionId) {
		ResultVO result = new ResultVO();
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO.getHash(DIYKey.getSessionHashKey(sessionId),
				SystemUserSessionVO.class);
		List<ShopTechnicianBO> shopTechnicianBO = shopTechnicianDAO.getList(systemUserSessionVO.getSystemId());
		result.setData(shopTechnicianBO);
		return result;
	}

	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp, ShopTechnicianVO shopTechnicianVO) {
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO.getHash(
				DIYKey.getSessionHashKey(shopTechnicianVO.getSessionId()),
				SystemUserSessionVO.class);
		ShopTechnicianBO shopTechnicianBO = new ShopTechnicianBO();
		BeanUtils.copyProperties(shopTechnicianVO, shopTechnicianBO);
		shopTechnicianBO.setShopId(systemUserSessionVO.getSystemId());
		List<ShopTechnicianBO> list = shopTechnicianDAO.findAll(shopTechnicianBO,dtp.getStart(), dtp.getLength());
		int totalCount = shopTechnicianDAO.getTotalCount(shopTechnicianBO);
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}

	@Override
	public ResultVO add(ShopTechnicianVO shopTechnicianVO) {
		ResultVO result = new ResultVO();
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO.getHash(
				DIYKey.getSessionHashKey(shopTechnicianVO.getSessionId()),
				SystemUserSessionVO.class);
		
		ShopTechnician shopTechnician = new ShopTechnician();
		BeanUtils.copyProperties(shopTechnicianVO, shopTechnician);
		shopTechnician.setShopId(systemUserSessionVO.getSystemId());
		
		if (StringUtils.isNotBlank(shopTechnician.getPicture())) {
			boolean is = AliyunOSSClientUtil.moveFile(backetName,shopTechnician.getPicture(), shopTechnicianPath, uploadPath);
			if(is){
				AliyunOSSClientUtil.removeFile(backetName, uploadPath+shopTechnician.getPicture());
			}
		}
		
		shopTechnicianDAO.insert(shopTechnician);
		return result;
	}

	@Override
	public ResultVO del(String technicianId) {
		ResultVO result = new ResultVO();
		ShopTechnician shopTechnician = shopTechnicianDAO.query(technicianId);
		if (shopTechnician == null) {
			throw new ShopTechnicianNotFoundException("找不到该技师");
		}
		shopTechnicianDAO.updateToDelete(technicianId);
		return result;
	}

	@Override
	public ResultVO findById(String technicianId) {
		ResultVO result = new ResultVO();
		ShopTechnicianVO shopTechnicianVO=new ShopTechnicianVO();
		ShopTechnician shopTechnician = shopTechnicianDAO.query(technicianId);
		if (shopTechnician == null) {
			throw new ShopTechnicianNotFoundException("找不到该技师");
		}
		BeanUtils.copyProperties(shopTechnician, shopTechnicianVO);

		if (StringUtils.isNotBlank(shopTechnician.getWorkTime())) {
			String[] hours = shopTechnician.getWorkTime().split("-");
			if (hours.length >= 2) {
				shopTechnicianVO.setWorkStartTime(hours[0]);
				shopTechnicianVO.setWorkEndTime(hours[1]);
			}
		}
		if (StringUtils.isNotBlank(shopTechnician.getRestTime())) {
			String[] hours = shopTechnician.getRestTime().split("-");
			if (hours.length >= 2) {
				shopTechnicianVO.setRestStartTime(hours[0]);
				shopTechnicianVO.setRestEndTime(hours[1]);
			}
		}
		
		result.setData(shopTechnicianVO);
		return result;
	}

	@Override
	public ResultVO update(ShopTechnicianVO shopTechnicianVO) {
		ResultVO result = new ResultVO();
		ShopTechnician shopTechnician = shopTechnicianDAO.query(shopTechnicianVO.getId());
		
		if (StringUtils.isNotBlank(shopTechnicianVO.getPicture())) {
			boolean is = AliyunOSSClientUtil.moveFile(backetName,shopTechnicianVO.getPicture(), shopTechnicianPath, uploadPath);
			if(is){
				AliyunOSSClientUtil.removeFile(backetName, uploadPath+shopTechnicianVO.getPicture());
			}
		}
		BeanUtils.copyProperties(shopTechnicianVO, shopTechnician);
		
		shopTechnicianDAO.update(shopTechnician);
		return result;
	}

	@Override
	public ResultVO delPicture(String picture, String technicianId) {
		ResultVO result = new ResultVO();
		ShopTechnician shopTechnician = shopTechnicianDAO.query(technicianId);
		if (shopTechnician == null) {
			throw new ShopTechnicianNotFoundException("找不到该技师");
		}
		shopTechnician.setPicture(null);
		shopTechnicianDAO.update(shopTechnician, true);

		AliyunOSSClientUtil.removeFile(backetName, shopTechnicianPath+picture);
		return result;
	}
}
